<!-- 新增分区 -->
<template>
  <div>
    <el-dialog title="废止合同" :visible.sync="dialogShow" width="65%" :close-on-click-modal="false" :close-on-press-escape="false">
      <form-common v-if="abolishFiledList.length > 0" :filed-list="abolishFiledList" ref="abolishForm"></form-common>
      <div class="flex margin-bottom-20">
        <p class="left-lable font-size-16 padding-left-right-10 margin-top-5 required font-weight-400 color-333333" style="width: 165px">审批文件</p>
        <uploadFile :is-multiple="true" :limit="10" filed-name="file" :max-size="20" accept-file-type=".xlsx,.xls,.csv,.png,.jpg,.Png,.Jpg,.mp4,.pdf" :file-list="fileList" @handleSuccess="handleSuccess" file-url-name="fileUrl" file-txt-name="fileTitle"> </uploadFile>
      </div>
      <div v-if="contractType == '7' || contractType == '8' || contractType == '9' || contractType == '10' || contractType == '11' || contractType == '12'" class="color-red margin-top-bottom-10 padding-left-10">请先完成付款再进行废止流程</div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogShow = false" class="el-button-reset">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import formCommon from '@g/components/form/formCommon.vue';
  import { leaseFiledList, disburseFiledList } from './abolishContract.js';
  import uploadFile from '../../projectInfo/modules/upload.vue';
  import { apiAbolishContracrt,apiInitiate } from '../../../api/contract';
  import { apiCreateAudit } from '../../../api/common';
  import { requestTips, askDialog } from '../../../assets/util';

  export default {
    props: {
      contractType: {
        type: String,
        default: '',
      }
    },
    data() {
      return {
        dialogShow: false,
        abolishFiledList: [],
        fileList: [],
      };
    },

    watch: {
      contractType: {
        handler(val) {
          console.log(val);
          if (val == 'ZX' || val == 'GC' || val == 'TG' || val == 'XZ' || val == 'CW' || val == 'YY') {
            this.abolishFiledList = disburseFiledList; //支出类
          } else {
            this.abolishFiledList = leaseFiledList;
          }
        },
      },
    },

    mounted () {
      this.getInitiate()
    },

    methods: {
      //发起方数据源
      async getInitiate() {
          let res = await apiInitiate({ contractKey: this.$route.query.contractKey,status:1 });
          leaseFiledList.forEach(item=>{
            if (item.bindFieldName == 'initiator') {
              item.optionList = res.rows || []
            }
          })
          disburseFiledList.forEach(item=>{
            if (item.bindFieldName == 'initiator') {
              item.optionList = res.rows || []
            }
          })
      },
      async confirmFn() {
        let userInfo = JSON.parse(localStorage.getItem('userInfo'));
        let model = this.$refs.abolishForm.validateForm();
        if (!model) return;
        this.$confirm('确认提交？', '提示', {
          distinguishCancelAndClose: true,
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          customClass:"bigZindex"
        })
        .then(async() => {
          model.contractKey = this.$route.query.contractKey;
          model.attachmentList = this.fileList;
          model.applyUser = userInfo.key;
          model.applyUserName = userInfo.nickname;
          model.projectKey = this.$parent.getProjectKey();
          model.orgKey = this.$parent.getOrgKey();
          let res = await apiAbolishContracrt(model);
          if (res.success) {
            this.submitFn(res.key);
          } else {
            this.$message.error(res.message);
          }
        })
        .catch(action => {});
      },
      //提审
      async submitFn(key) {
        let userInfo = JSON.parse(localStorage.getItem('userInfo'));
        let projectKey = this.$parent.getProjectKey();
        let param = {
          businessKey: key, //废止合同保存的主键
          userPrimaryKey: userInfo.key,
          projectKey: projectKey,
          type: '合同终止', //合同,账单,付款,合同终止
          typeKey: 'contractTermination', //contractSubmit,bill,payment,contractTermination
        };
        let res = await apiCreateAudit(param);
        requestTips(res, async () => {
          setTimeout(() => {
            this.$emit('getInfo');
          }, 1000);
          this.dialogShow = false;
        });
      },
      //上传成功
      handleSuccess(file, fileList) {
        if (file.data.rows && file.data.rows.length > 0) {
          this.fileList.push({
            fileId: file.data.rows[0].key,
            fileUrl: file.data.rows[0].url,
            fileTitle: file.data.rows[0].originalName,
            fileDesc: file.data.rows[0].originalName,
          });
        } else {
          this.fileList = [];
          this.$message.error(file.data.message);
        }
      },
    },

    components: { formCommon, uploadFile },
  };
</script>
<style lang="scss" scoped></style>
